package com.app.twth.ui.b2cpick;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityB2cPickBinding;
import com.app.twth.ui.b2cpick.oblpn.B2CPickOBLPNActivity;
import com.app.twth.utils.EditTextUtil;

import java.util.ArrayList;

/**
 * @describe :
 */
public class B2CPickActivity extends BaseActivity<ActivityB2cPickBinding, B2CPickViewModel> {
    private String TAG = getClass().getSimpleName();
    private B2CPickAdapter pickAapter;

    @Override
    protected void initListener() {
        viewModel.pickListLiveData.observe(this, data -> {
            pickAapter.setData(data, 0);
        });
        viewModel.pickLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("B2CPick", data);
            AppRouter.showActivityForResult(mContext, B2CPickOBLPNActivity.class, bundle, Constant.ForResult2);
        });

        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Pick(Job)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        pickAapter = new B2CPickAdapter(this, new ArrayList<>(), position -> {
            viewModel.b2cPickCheckPickNo(viewModel.pickListLiveData.getValue().get(position));
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(pickAapter);

        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e(TAG, "onResume");
        bottomLayoutBinding.tvHint.setText("");
        viewModel.b2cPickPendingJobs();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {

        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        judgePickNo();
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                EditTextUtil.hideInput(mContext);
                judgePickNo();
            }
            return false;
        }
    };

    private void judgePickNo() {
        new AsyncTask<String, Integer, B2CPick>() {

            @Override
            protected B2CPick doInBackground(String... strings) {
                if (viewModel.pickListLiveData.getValue() == null) {
                    return null;
                }
                for (B2CPick pick : viewModel.pickListLiveData.getValue()) {
                    if (pick.getPickNo().equalsIgnoreCase(barcodeStr)) {
                        return pick;
                    }

                }
                return null;
            }

            @Override
            protected void onPostExecute(B2CPick pick) {
                super.onPostExecute(pick);
                if (pick != null) {
                    viewModel.b2cPickCheckPickNo(pick);
                } else {
                    showHint("Pick list does not exist or has been completed");
                }
            }
        }.execute();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
